package sort;

import org.junit.Test;

import java.util.Arrays;

/**
 * 希尔排序
 *
 * @author DuJiabao
 * @create 2020-12-14 17:42
 */
public class ShellSort extends Example {
    public static void sort(Comparable[] array) {
        for (int increment = array.length / 2; increment > 0; increment /= 2) {
            for (int i = increment; i < array.length; i++) {
                Comparable temp = array[i];
                int j;
                for (j = i; j >= increment && less(temp, array[j - increment]); j -= increment) {
                    array[j] = array[j - increment];
                }
                array[j] = temp;
            }
        }
    }

    @Test
    public void test() {
        long currentTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            Integer[] arr = new Integer[]{10, -1, 2, 11, 23, -100, 40, 20, 20, 30, 40, 10, 30, 30, 22, 30, 10, 2, 3, 2, 10, 20};
            sort(arr);
        }
        Integer[] arr = new Integer[]{10, -1, 2, 11, 23, -100, 40, 20, 20, 30, 40, 10, 30, 30, 22, 30, 10, 2, 3, 2, 10, 20};
        sort(arr);
        System.out.println("isSorted: " + isSorted(arr));
        System.out.println("Use time: " + (System.currentTimeMillis() - currentTime));
        show(arr);
    }
}
